package com.cooper.dao.trade;

import java.util.List;

import com.cooper.entity.OrderInfo;
import com.cooper.pagination.PageInfo;
import com.cooper.vo.OrderSearchVo;

import net.paoding.rose.jade.annotation.DAO;
import net.paoding.rose.jade.annotation.Insert;
import net.paoding.rose.jade.annotation.ReturnGeneratedKeys;
import net.paoding.rose.jade.annotation.SQL;

@DAO
public interface OrderInfoDAO {
	
	@ReturnGeneratedKeys
	@Insert(tableName="td_order_info")
	Long addOrderInfo(OrderInfo orderInfo) throws Exception;
	
	@SQL("select * from td_order_info where deleted='n' and id=:1 ")
	OrderInfo getOrderInfo(Long id) throws Exception;
	
	@SQL("select * from td_order_info where deleted='n' and order_num=:1 ")
	OrderInfo getOrderInfo(String orderNum) throws Exception;
	
	@SQL("select count(*) from td_order_info t where deleted='n' and user_deleted=:1.userDeleted and user_id=:1.userId"
			+ " #if(null!=:1.tradeStatus && ''!=:1.tradeStatus){ and trade_status=:1.tradeStatus } "
			+ " #if(null!=:1.orderNum && ''!=:1.orderNum){ and order_num=:1.orderNum } "
			+ " #if(0!=:1.statusList.size()){ and trade_status in (:1.statusList) } "
			+ " #if(null!=:1.itemTitle){ and exists(select id from td_order_item a where deleted='n' and t.order_num=a.order_num and item_title like '%'||:1.itemTitle||'%')} ")
	int queryOrderInfoCountForBuyer(OrderSearchVo searchVo);
	
	@SQL("select count(*) from td_order_info t where deleted='n' "
			+ " #if(null !=:1.sellerGroupId){ and seller_group_id=:1.sellerGroupId} "
			+ " #if(null!=:1.tradeStatus && ''!=:1.tradeStatus){ and trade_status=:1.tradeStatus } "
			+ " #if(null!=:1.orderNum && ''!=:1.orderNum){ and order_num=:1.orderNum } "
			+ " #if(0!=:1.statusList.size()){ and trade_status in (:1.statusList) } "
			+ " #if(null!=:1.itemTitle){ and exists(select id from td_order_item a "
			+ "where deleted='n' and t.order_num=a.order_num and item_title like '%'||:1.itemTitle||'%')} ")
	int queryOrderInfoCountForSeller(OrderSearchVo searchVo);
	
	@SQL("select order_num from td_order_info t where deleted='n' and user_deleted=:1.userDeleted and user_id=:1.userId "
			+ " #if(null!=:1.tradeStatus && ''!=:1.tradeStatus){ and trade_status=:1.tradeStatus } "
			+ " #if(null!=:1.orderNum && ''!=:1.orderNum){ and order_num=:1.orderNum } "
			+ " #if(0!=:1.statusList.size()){ and trade_status in (:1.statusList) } "
			+ " #if(null!=:1.itemTitle){ and exists(select id from td_order_item a where deleted='n' and t.order_num=a.order_num and item_title like '%'||:1.itemTitle||'%')} "
			+ " order by gmt_create desc limit :2.startIndex,:2.pageSize ")
	List<String> queryOrderNumListForBuyer(OrderSearchVo searchVo, PageInfo pageInfo);
	
	@SQL("select order_num from td_order_info t where deleted='n' "
			+ " #if(null !=:1.sellerGroupId){ and seller_group_id=:1.sellerGroupId} "
			+ " #if(null!=:1.tradeStatus && ''!=:1.tradeStatus){ and trade_status=:1.tradeStatus } "
			+ " #if(null!=:1.orderNum && ''!=:1.orderNum){ and order_num=:1.orderNum } "
			+ " #if(0!=:1.statusList.size()){ and trade_status in (:1.statusList) } "
			+ " #if(null!=:1.itemTitle){ and exists(select id from td_order_item a where deleted='n' and t.order_num=a.order_num and item_title like '%'||:1.itemTitle||'%')} "
			+ " order by gmt_create desc limit :2.startIndex,:2.pageSize ")
	List<String> queryOrderNumListForSeller(OrderSearchVo searchVo, PageInfo pageInfo);
}
